import java.io.*;
import java.net.*;

/**
 * 简单的TCP测试服务器
 * 用于测试小车控制客户端
 */
public class TestTCPServer {
    private static final int PORT = 6000;
    
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("TCP测试服务器启动，监听端口: " + PORT);
            
            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端连接: " + clientSocket.getInetAddress().getHostAddress());
                
                // 为每个客户端创建一个新线程
                new Thread(() -> handleClient(clientSocket)).start();
            }
        } catch (IOException e) {
            System.err.println("服务器启动失败: " + e.getMessage());
        }
    }
    
    private static void handleClient(Socket clientSocket) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
            
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("收到消息: " + inputLine);
                
                // 模拟小车响应
                String response = "ACK:" + inputLine;
                out.println(response);
                System.out.println("发送响应: " + response);
            }
        } catch (IOException e) {
            System.err.println("处理客户端连接时出错: " + e.getMessage());
        } finally {
            try {
                clientSocket.close();
                System.out.println("客户端连接已关闭");
            } catch (IOException e) {
                System.err.println("关闭客户端连接时出错: " + e.getMessage());
            }
        }
    }
} 